﻿using System;
using System.Net;
using Silvery.Role.Simple;
using Silvery.Loader;
using System.Windows;
namespace Silvery.Role.Standard
{
    public class StandardRole : SimpleRole
    {
        private RoleDirection _RoleDir = RoleDirection.Down;
        public RoleDirection RoleDir
        {
            get { return _RoleDir; }
            set
            {
                if (_RoleDir == value)
                    return;
                _RoleDir = value; 
                SetRoleAnimat(this, _RoleDir, _RoleState);
            }
        }
        RoleState _RoleState = RoleState.Wait;
        public RoleState RoleState 
        {
            get { return _RoleState; }
            set
            {
                if (_RoleState == value)
                    return;
                _RoleState = value; 
                SetRoleAnimat(this, _RoleDir, _RoleState);
            }
        }
        public static void SetRoleAnimat(IAnimat spirit, RoleDirection dir, RoleState state)
        {
            spirit.CurrentAnimatName = state.ToString() + dir.ToString();
        }
        public StandardRole(Uri bodyUri)
            :base(bodyUri)
        {
            
        }
        protected override void SetBodyDownLoadUri(Uri bodyUri)
        {
            if (bodyUri != null)
            {
                spiritloader = new StandardRolePartLoader(bodyUri);
                spiritloader = LoaderManager.Current.Load(spiritloader);
                spiritloader.DownloadCompleted += new OpenReadCompletedEventHandler(spiritloader_DownloadCompleted);
            }
            base.BeginDownload();
        }
        void spiritloader_DownloadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            (sender as ILoader).DownloadCompleted -= spiritloader_DownloadCompleted;
            OnBodyPackLoaded(this,(sender as SpiritLoader).SpiritData);
        }
        protected override void OnBodyPackLoaded()
        {
            base.OnBodyPackLoaded();
            CurrentAnimatName = RoleState.Wait.ToString() + RoleDirection.Down.ToString();
        }
        public override void MoveTo(Point from, Point to, double speed = GlobleConfig.DefaultObjectMoveCost)
        {
            base.MoveTo(from, to, speed);
            this.RoleDir = RoleDirectionHelper.GetRoleDirectionFormTowPoint(from, to);
            this.RoleState = Silvery.Role.RoleState.Run;
            
        }
        protected override void OnMoveCompleted()
        {
            base.OnMoveCompleted();
            this.RoleState = Silvery.Role.RoleState.Wait;
        }
    }
}
